/*
 * @(#)GSProductInfoDownServlet.java   
 *
 * Project: CUPSecureBM
 * 
 * Modify Information:
 *  Author          Date                Description
 * ==============   =================   =====================
 *  yonggangguo     2005-12-29             Create this file
 *  Tuthmos Liu     2008-06-16           增加图件号
 * Copyright notice:
 *   Copyright (c) 2001-2005 Beijing HiTRUST Technology Co., Ltd.
 *   1808 Room, Science & Technology Building, No. 9 South Street,
 *   Zhong Guan Cun, Haidian District, Beijing ,100081, China
 *   All rights reserved.
 *
 *   This software is the confidential and proprietary information of
 *   HiTRUST.COM, Inc. ('Confidential Information').  You shall not
 *   disclose such Confidential Information and shall use it only in
 *   accordance with the terms of the license agreement you entered
 *   into with HiTRUST.
 */

package com.hitrust.gs.action;

import java.io.OutputStream;
import java.util.ArrayList;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import system.base.Product;
import system.base.ProductUpdatePrice;
import system.base.ProductUpdatePriceSearchEvent;
import system.base.Warehouse;

import com.hitrust.gs.business.GSOrderProcesser;
import com.hitrust.gs.lib.GSLogWriter;
import com.hitrust.gs.service.GSBaseManager;
import com.hitrust.gs.util.page.ResultProcess;
import com.hitrust.gs.vo.GSOrderAuditQueryVO;

import framework.core.DBConnection;
import framework.core.ObjectList;

public class GSProductStorageDownServlet extends HttpServlet {
	 private GSOrderProcesser ordProcesser = new GSOrderProcesser();
	 
	public void init(ServletConfig aConfig) throws ServletException {
		super.init(aConfig);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException {
		response.setContentType("application/excel;charset=UTF-16");
		response.setHeader("Content-disposition",
				"attachment; filename=productStorageView.xls");
		response.setHeader("Content-Description", "智能补货信息");

		DBConnection dbcon = null;
		OutputStream tOutputStream = null;
	
		try {
			dbcon = DBConnection.open();
			tOutputStream = response.getOutputStream();
			GSOrderAuditQueryVO oaqVO = (GSOrderAuditQueryVO)request.getSession().getAttribute("oaqVO");
	         ArrayList orderAryAll = ordProcesser.getProductSotrageViewAllList(oaqVO);

			DBConnection con = null;
			con = DBConnection.open(false);
			String warehouseName = Warehouse.fetchByCode(con, oaqVO.deptId).getName();
			con.close();
			writeExcel(orderAryAll, tOutputStream,warehouseName);

		} catch (Exception e) {
			GSLogWriter.error(e.getMessage());
		} finally {
			try {
				if (tOutputStream != null) {
					tOutputStream.close();
				}
				dbcon.close();
			} catch (Exception e) {
			}
		}
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException {
		doPost(request, response);
	}

	private void writeExcel(ArrayList aList, OutputStream tOutputStream,String warehouseName) {

		DBConnection con = null;
		try {
			con = DBConnection.open();
			HSSFWorkbook tWb = new HSSFWorkbook();
			HSSFSheet tSheet = tWb.createSheet();
			tWb.setSheetName(0, "智能补货", HSSFCell.ENCODING_UTF_16);
			HSSFRow row = tSheet.createRow((short) 0);
			createCell(tWb, row, (short) 6, warehouseName+"仓库采购参照表 ", HSSFCellStyle.ALIGN_CENTER);
			
			HSSFRow tRow = tSheet.createRow((short) 1);
			createCell(tWb, tRow, (short) 0, "仓库 ", HSSFCellStyle.ALIGN_CENTER);
			createCell(tWb, tRow, (short) 1, "集中编码", HSSFCellStyle.ALIGN_CENTER);
			createCell(tWb, tRow, (short) 2, "产品名称", HSSFCellStyle.ALIGN_CENTER);
			createCell(tWb, tRow, (short) 3, "供应商", HSSFCellStyle.ALIGN_CENTER);
			createCell(tWb, tRow, (short) 4, "规格", HSSFCellStyle.ALIGN_CENTER);
			createCell(tWb, tRow, (short) 5, "型号", HSSFCellStyle.ALIGN_CENTER);
			createCell(tWb, tRow, (short) 6, "品牌", HSSFCellStyle.ALIGN_CENTER);
			createCell(tWb, tRow, (short) 7, "价格（元）", HSSFCellStyle.ALIGN_CENTER);
			createCell(tWb, tRow, (short) 8, "单位", HSSFCellStyle.ALIGN_CENTER);
			createCell(tWb, tRow, (short) 9, "库存数量", HSSFCellStyle.ALIGN_CENTER);
			createCell(tWb, tRow, (short) 10, "安全库存", HSSFCellStyle.ALIGN_CENTER);
			createCell(tWb, tRow, (short) 11, "最大库存", HSSFCellStyle.ALIGN_CENTER);
			createCell(tWb, tRow, (short) 12, "建议补货数量",
					HSSFCellStyle.ALIGN_CENTER);
			createCell(tWb, tRow, (short) 13, "前三月平均出库数量",
					HSSFCellStyle.ALIGN_CENTER);
			createCell(tWb, tRow, (short) 14, "结算模式", HSSFCellStyle.ALIGN_CENTER);

			for (int i = 0; i < aList.size(); i++) {
				tRow = tSheet.createRow((short) (i + 2));
				String[] tStr=(String[])aList.get(i);
			
				createCell(tWb, tRow, (short) 0, tStr[1]);
				createCell(tWb, tRow, (short) 1, tStr[2]);
				createCell(tWb, tRow, (short) 2, tStr[3]);
				createCell(tWb, tRow, (short) 3, tStr[8]);
				createCell(tWb, tRow, (short) 4, tStr[5]);
				createCell(tWb, tRow, (short) 5, tStr[6]);
				createCell(tWb, tRow, (short) 6, tStr[7]);
				createCell(tWb, tRow, (short) 7, tStr[9]);
				createCell(tWb, tRow, (short) 8, tStr[4]);
				createCell(tWb, tRow, (short) 9, tStr[10]);
				createCell(tWb, tRow, (short) 10, tStr[15]);
				createCell(tWb, tRow, (short) 11, tStr[16]);
				createCell(tWb, tRow, (short) 12, tStr[14]);
				createCell(tWb, tRow, (short) 13, tStr[11]);
				createCell(tWb, tRow, (short) 14, tStr[12]);
			}
			tWb.write(tOutputStream);
			tOutputStream.flush();

		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			try {
				con.close();
			} catch (Exception e) {
			}
		}
	}

	private void createCell(HSSFWorkbook aWb, HSSFRow aRow, short column,
			String aValue) {
		HSSFCell tCell = aRow.createCell(column);
		tCell.setEncoding(HSSFCell.ENCODING_UTF_16);
		tCell.setCellValue(aValue);
	}

	private void createCell(HSSFWorkbook aWb, HSSFRow aRow, short column,
			String aValue, short aAlign) {
		HSSFCell tCell = aRow.createCell(column);
		tCell.setEncoding(HSSFCell.ENCODING_UTF_16);
		tCell.setCellValue(aValue);
		HSSFCellStyle tCellStyle = aWb.createCellStyle();
		tCellStyle.setAlignment(aAlign);
		tCell.setCellStyle(tCellStyle);
	}
}
